# -*- coding: utf-8 -*-
# This code written by ChanGOGOGO
"""
对话设计页面元素
"""
from pages.base_page import BasePage


class DialogDesignMainPage(BasePage):
    def __init__(self, page):
        super().__init__(page)

    def tab(self, tab_name="独立场景"):
        """[独立场景]和[实验组]tab页"""
        element = self.page.locator(
            f"//div[@role='tablist']//div[@role='tab' and contains(text(), '{tab_name}')]"
        )
        return element

    def scene_inside_tab(self, tab_name):
        """场景/实验组详情内部的tab页：流程设计、全局事件...会话调试"""
        element = self.page.locator(
            f"//div[@class='processdesign-headernav']//ul/li/a[text()='{tab_name}']"
        )
        return element

    def publish_scene_button(self):
        """【发布场景】按钮"""
        element = self.page.locator(
            "//button[@title='发布任务图']//*[name()='svg']/*[name()='use']"
        )
        return element

    def publich_experiment_group(self):
        """【发布实验组】按钮"""
        xpath = "//button[@title='发布实验组']//*[name()='svg']/*[name()='use']"
        element = self.page.locator(xpath)
        return element

    def back_button(self):
        """场景详情左上角【返回】按钮"""
        xpath = f"//a[@class='nav-back']"
        element = self.page.locator(xpath)
        return element

    def toast_message(self, message):
        """toast提示信息"""
        # xpath = "//div[@role='alert']/p[@class='el-message__content']"
        # element = self.page.locator(xpath)
        element = self.page.locator(f"text='{message}'")
        return element


class IndependentScenePage(BasePage):
    """独立场景页面"""

    def __init__(self, page):
        super().__init__(page)

    def search_scene_input(self):
        """场景名称查询输入框。"""
        element = self.page.locator(
            "//div[@aria-labelledby='tab-single']//input[@placeholder='搜索场景名称']"
        )
        return element

    def create_scene_button(self):
        """【新建场景】按钮"""
        element = self.page.get_by_role("button", name="新建场景")
        return element

    def scene_name_input(self):
        """新建场景窗口的“场景名称”输入框"""
        element = self.page.locator(
            "//div[@aria-label='新建独立场景']//form//input[@placeholder='请输入场景名称']"
        )
        return element

    def create_method_radio(self, create_method="创建新场景"):
        """新建场景窗口的“创建方式”单选按钮"""
        element = self.page.locator(
            f"//div[@aria-label='新建独立场景']//form//label[@role='radio']//span["
            f"normalize-space()='{create_method}']"
        )
        return element

    def scan_button(self):
        """创建方式选择【导入】时，文件上传的【浏览】按钮"""
        element = self.page.get_by_role("button", name="浏览")
        return element

    def reuse_scene_dropbox(self):
        """创建方式选择【复用现有场景】时，复用场景下拉框"""
        element = self.page.locator(
            "//label[text()='复用场景']/following-sibling::div//"
            "input[@placeholder='选择场景']"
        )
        return element

    def scene_type_span(self, scene_type="独立场景"):
        """复用场景下拉框的一级选项：独立场景、实验组"""
        element = self.page.locator(f"//div[@role='menu']//span[text()='{scene_type}']")
        return element

    def scene_name_span(self, scene_name):
        """复用场景下拉框的二级选项：独立场景名称、实验组名称"""
        element = self.page.locator(f"//div[@role='menu']//span[text()='{scene_name}']")
        return element

    def child_scene_name_span(self, child_scene_name):
        """复用场景下拉框的三级选项：实验组子场景名称"""
        element = self.page.locator(
            f"//div[@role='menu']//span[text()='{child_scene_name}']"
        )
        return element

    def button(self, button_name="确认"):
        """新建场景窗口的按钮，包含【确认】【取消】按钮"""
        element = self.page.get_by_role("button", name=button_name)
        return element


class ExperimentGroupPage(BasePage):
    def __init__(self, page):
        super().__init__(page)
        self._create_child_scene_window = self.create_child_scene_window()
        self._create_experiment_group_window = self.create_experiment_group_window()

    def search_scene_input(self):
        """场景名称查询输入框"""
        element = self.page.locator(
            "//div[@aria-labelledby='tab-collection']//input[@placeholder='搜索场景名称']"
        )
        return element

    def create_experiment_group_button(self):
        """【新建实验组】按钮"""
        element = self.page.get_by_role("button", name="新建实验组")
        return element

    def create_experiment_group_window(self):
        """新建实验组窗口"""
        xpath = "//div[@aria-label='新建实验组']"
        element = self.page.locator(xpath)
        return element

    def experiment_group_name_input(self):
        """新建实验组窗口中的'实验组名称'输入框"""
        xpath = "//label[text()='实验组名称']/following-sibling::div//input[@placeholder='请输入实验组名称']"
        elemnent = self._create_experiment_group_window.locator(xpath)
        return elemnent

    def create_experiment_group_child_scene_button(self, experiment_group_name):
        """实验组【新建子场景】按钮"""
        xpath = (
            f"//div[@class='scene-container']//span[@class='collection-title' and "
            f"text()='{experiment_group_name}']/following-sibling::div/button[@title='新建子场景']"
        )
        element = self.page.locator(xpath)
        return element

    def experiment_group_name(self, experiment_group_name):
        """实验组名称"""
        xpath = f"//div[@class='scene-container']//span[@class='collection-title' and text()='{experiment_group_name}']"
        element = self.page.locator(xpath)
        return element

    def child_scene_name(self, experiment_group_name, child_scene_name):
        """实验组下子场景名称"""
        xpath = (
            f"//div[@class='scene-container']//span[@class='collection-title' and "
            f"text()='{experiment_group_name}']/ancestor::div[contains(@class, 'scene-collection-container')]"
            f"//div[@class='scene-item']//a[text()='{child_scene_name}']"
        )
        element = self.page.locator(xpath)
        return element

    def create_child_scene_window(self):
        """新建子场景窗口父元素"""
        xpath = "//div[@aria-label='新建场景']"
        element = self.page.locator(xpath)
        return element

    def scene_name_input(self):
        """新建子场景窗口：“场景名称”输入框"""
        xpath = "//form//input[@placeholder='请输入场景名称']"
        element = self._create_child_scene_window.locator(xpath)
        return element

    def create_method_radio(self, create_method="创建新场景"):
        """新建子场景窗口：“创建方式”单选按钮"""
        xpath = (
            f"//form//label[@role='radio']//span[normalize-space()='{create_method}']"
        )
        element = self._create_child_scene_window.locator(xpath)
        return element

    def scan_button(self):
        """创建方式选择【导入】时，文件上传的【浏览】按钮"""
        element = self.page.get_by_role("button", name="浏览")
        return element

    def reuse_scene_dropbox(self):
        """创建方式选择【复用现有场景】时，复用场景下拉框"""
        element = self.page.locator(
            "//label[text()='复用场景']/following-sibling::div//"
            "input[@placeholder='选择场景']"
        )
        return element

    def scene_type_span(self, scene_type="独立场景"):
        """复用场景下拉框的一级选项：独立场景、实验组"""
        element = self.page.locator(f"//div[@role='menu']//span[text()='{scene_type}']")
        return element

    def scene_name_span(self, scene_name):
        """复用场景下拉框的二级选项：独立场景名称、实验组名称"""
        element = self.page.locator(f"//div[@role='menu']//span[text()='{scene_name}']")
        return element

    def child_scene_name_span(self, child_scene_name):
        """复用场景下拉框的三级选项：实验组子场景名称"""
        element = self.page.locator(
            f"//div[@role='menu']//span[text()='{child_scene_name}']"
        )
        return element

    def generic_config_button(self, experiment_group_name):
        """实验组【通用设置】按钮"""
        xpath = (
            f"//span[text()='{experiment_group_name}']/following-sibling::div"
            f"/button[contains(@title, '通用配置')]/span"
        )
        element = self.page.locator(xpath)
        return element

    def button(self, button_name="确认"):
        """新建场景窗口的按钮，包含【确认】【取消】按钮"""
        element = self.page.get_by_role("button", name=button_name)
        return element


class SceneCard(BasePage):
    def __init__(self, page):
        super().__init__(page)

    def scene_card(self, scene_name):
        """场景卡片。可定位独立场景卡片、实验组子场景卡片"""
        element = self.page.locator(
            f"//div[@class='scene-item-list']//a[text()='{scene_name}']"
        )
        return element

    def more_button(self, scene_name):
        """场景卡片右上角【更多】按钮"""
        element = self.page.locator(
            f"//div[@class='scene-item-list']//a[text()='{scene_name}']"
            f"/ancestor::ul/following-sibling::div//button[@title='更多']"
        )
        return element

    def more_operator_button(self, button_name):
        """场景卡片点击【更多】后展示的其他操作按钮：复制场景、重命名、删除、导出"""
        element = self.page.locator(f"//body/ul/li[normalize-space()='{button_name}']")
        return element

    def child_scene(self, experiment_group_name, child_scene_name):
        """实验组下的子场景"""
        xpath = (
            f"//span[text()='{experiment_group_name}']/ancestor::div[@class='scene-collection-container "
            f"child-scene']//div[@class='scene-item-list']//a[text()='{child_scene_name}']"
        )
        element = self.page.locator(xpath)
        return element


class ProcessDesignPage(BasePage):
    """[流程设计]tab页"""

    def __init__(self, page):
        super().__init__(page)

    def node(self, node_name):
        """流程设计中的节点"""
        element = self.page.locator(
            f"//main//*[name()='svg']/*[name()='g']//p[text()='{node_name}']"
        )
        return element

    def node_edit_button(self, node_name):
        """澄清节点、挂机节点方块上的【编辑】按钮"""
        xpath = (
            f"//p[text()='{node_name}']/ancestor::*[name()='foreignObject']"
            f"/following-sibling::*[name()='use' and @href='#yiflow-bianji1']"
        )
        element = self.page.locator(xpath)
        return element

    def node_more_button(self, node_name):
        """澄清节点、挂机节点方块上的【更多】按钮"""
        element = self.page.locator(
            f"//p[text()='{node_name}']/ancestor::*[name()='foreignObject']/"
            f"following-sibling::*[name()='use' and @href='#yiflow-gengduo2']"
        )
        return element


class AddJumpBranch(BasePage):
    """添加分支作为一个公共木块，API节点、澄清节点都需要用到"""

    def __init__(self, page):
        super().__init__(page)
        self.dump_branch_div = self.page.locator(
            "//div[normalize-space()='添加分支']/parent::div"
        )

    def branch_name_input(self):
        """分支名称输入框"""
        element = self.dump_branch_div.get_by_role("textbox", name="请输入分支名称")
        return element

    def condition_name_dropbox(self):
        """条件名称下拉框"""
        element = self.dump_branch_div.locator(
            "//div[@class='condition-editor-container_"
            "_content-body__item-row']//input"
        )
        return element

    def condition_name_option(self, condition_name):
        """
        条件名称下拉框可选项。
        API节点跳转分支：接口调用、全局参数、任意文本；
        澄清节点跳转分支：意图、词槽、全局参数、节点停留、任意文本。
        :param condition_name:
        :return:
        """
        element = self.page.locator(
            f"//div[@class='el-scrollbar']//span[text()='{condition_name}']"
        )
        return element

    def interface_call_result_dropbox(self):
        """接口调用结果下拉框"""
        element = self.dump_branch_div.locator(
            "//div[@class='condition-editor-container_"
            "_content-body__item-container']"
        )
        return element

    def interface_call_result_option(self, call_result="调用成功"):
        """接口调用结果下拉框可选项：调用成功、调用失败"""
        element = self.page.locator(f"//span[text()='{call_result}']")
        return element

    def intent_condition_dropbox(self):
        """意图条件下拉框"""
        element = self.dump_branch_div.locator(
            "//div[@class='el-form-item condition-editor-container__"
            "content-body__item-container-condition is-required']//input"
        )
        return element

    def intent_condition_option(self, condition=""):
        """意图条件下拉框可选项：是、不是"""
        element = self.dump_branch_div.locator(f"//span[text()='{condition}']")
        return element

    def intent_dropbox(self):
        """意图名称下拉框"""
        element = self.dump_branch_div.locator(
            "//div[@class='el-form-item condition-editor-container__"
            "content-body__item-container__right is-required']//input"
        )
        return element

    def intent_option(self, intent_name):
        """意图名称可选项"""
        element = self.dump_branch_div.locator(f"//span[text()='{intent_name}']")
        return element

    def solt_dropbox(self):
        """词槽名称下拉框"""
        element = self.dump_branch_div.locator(
            "//div[@class='el-form-item slot-list is-required']//input"
        )
        return element

    def solt_option(self, solt_name):
        """词槽名称可选项"""
        element = self.page.locator(
            f"//div[@class='el-scrollbar']//span[text()='{solt_name}']"
        )
        return element

    def solt_condition_dropbox(self):
        """词槽条件下拉框"""
        element = self.dump_branch_div.locator(
            "//div[@class='el-form-item slot-condition is-required']//input"
        )
        return element

    def solt_condition_option(self, condition):
        """词槽条件可选项"""
        element = self.page.locator(
            f"//div[@class='el-scrollbar']//span[text()='{condition}']"
        )
        return element

    def global_param_input(self):
        """全局参数输入框"""
        element = self.dump_branch_div.locator(
            "//div[@class='el-form-item params-key is-required']//input"
        )
        return element

    def global_param_condition_dropbox(self):
        """全局参数条件下拉框"""
        element = self.dump_branch_div.locator(
            "//div[@class='el-form-item params-condition is-required']" "//input"
        )
        return element

    def global_param_condition_option(self, condition):
        """全局参数条件可选项"""
        element = self.dump_branch_div.locator(f"//span[text()='{condition}']")
        return element

    def node_stay_dropbox(self):
        """节点停留下拉框"""
        element = self.page.locator(
            "//div[@class='condition-editor-container__content-body__item-container']//input"
        )
        return element

    def node_stay_option(self, option_name):
        """节点停留下拉框可选项：1轮后、2轮后、3轮后、4轮后、5轮后"""
        element = self.page.locator(
            f"//div[@class='el-scrollbar']//span[text()='{option_name}']"
        )
        return element

    def dump_node_dropbox(self):
        """跳转至XX节点下拉框"""
        element = self.dump_branch_div.locator(
            "//div[@class='condition-editor-container__content-operate"
            "__container-item']//input"
        )
        return element

    def dump_node_option(self, node_name):
        """跳转节点可选项"""
        element = self.page.locator(
            f"//div[@class='el-scrollbar']//span[text()='{node_name}']"
        )
        return element

    def button(self, button_name="保存"):
        """添加分支div层的【新建节点】【完成】【取消】【删除分支】按钮"""
        element = self.dump_branch_div.get_by_role("button", name=button_name)
        return element


class AddNodeWindow(BasePage):
    """点击【新建节点】弹出的新建节点窗口"""

    def __init__(self, page):
        super().__init__(page)
        self.add_node_div = self.page.locator("//div[@aria-label='新增节点']")

    def node_name_input(self):
        """节点名称输入框"""
        element = self.add_node_div.locator(
            "//label[text()='节点名称']/following-sibling::div//input"
        )
        return element

    def node_type_dropbox(self):
        """节点类型下拉框"""
        element = self.add_node_div.locator(
            "//label[text()='节点类型']/following-sibling::div//input"
        )
        return element

    def node_type_option(self, node_type="澄清节点"):
        """节点类型下拉框可选项：澄清节点、API节点、挂机节点"""
        element = self.page.locator(
            f"//ul[@class='el-scrollbar__view el-select-dropdown__list']//span[text("
            f")='{node_type}']"
        )
        return element

    def button(self, button_name="立即创建"):
        """【立即创建】【取消】按钮"""
        element = self.add_node_div.get_by_role("button", name=button_name)
        return element


class RecordSpeechWindow(BasePage):
    """录音话术选择窗口"""

    def __init__(self, page):
        super().__init__(page)
        self.record_window = self.page.locator(
            "//div[@aria-label='选择话术' and @role='dialog']"
        )

    def button(self, button_name):
        """录音窗口按钮：【新增话术】【添加】【取消】【选择】【删除】"""
        element = self.record_window.get_by_role("button", name=button_name)
        return element

    def record_speech_input(self):
        """录音话术输入框"""
        element = self.record_window.get_by_placeholder("请输入录音话术")
        return element

    def speech_choose_button(self, record_content):
        """话术内容对应的【选择】按钮"""
        element = self.record_window.locator(
            f"//span[text()='{record_content}']/following-sibling::span"
            f"//span[normalize-space()='选择']"
        )
        return element


class NormalSpeechBox(BasePage):
    """机器人话术&超轮回复话术"""

    def __init__(self, page, speech_type="机器人话术"):
        super().__init__(page)
        self.speech_type = speech_type

    def tag(self, tag_name):
        """标签：【禁止打断】【用户静默】..."""
        element = self.page.locator(
            f"//span[contains(text(),'{self.speech_type}')]/parent::div"
            f"/following-sibling::div//span[text()='{tag_name}']"
        )
        return element

    def filled_tag(self):
        """定位已经输入的标签"""
        element = self.page.locator(
            f"//span[contains(text(),'{self.speech_type}')]/parent::div"
            f"/following-sibling::div//div[@class='base-editor-cotainer__content']"
            f"//p/span"
        )
        return element

    def input(self, content):
        """向话术框输入文本"""
        filled_tag_count = (
            self.filled_tag().count()
        )  # 需要先确定话术框中已经填写的标签数量N
        for x in range(
            filled_tag_count
        ):  # 根据已经输入的标签数量N，将输入光标向右移动N次
            self.page.keyboard.press("ArrowRight")
        self.page.keyboard.type(content)


class HangupNodeSpeechBox(BasePage):
    """挂机节点回复话术框"""

    def __init__(self, page):
        super().__init__(page)

    def tag(self, tag_name):
        """标签：【禁止打断】【用户静默】..."""
        xpath = f"//div[contains(text(),'回复话术')]/following-sibling::div//span[text()='{tag_name}']"
        element = self.page.locator(xpath)
        return element

    def filled_tag(self):
        """定位已经输入的标签"""
        xpath = (
            "//div[contains(text(),'回复话术')]/following-sibling::div//div[@class='base-editor-cotainer__content']"
            "//p/span"
        )
        element = self.page.locator(xpath)
        return element

    def input(self, content):
        """向话术框输入文本"""
        filled_tag_count = (
            self.filled_tag().count()
        )  # 需要先确定话术框中已经填写的标签数量N
        for x in range(
            filled_tag_count
        ):  # 根据已经输入的标签数量N，将输入光标向右移动N次
            self.page.keyboard.press("ArrowRight")
        self.page.keyboard.type(content)


class ChooseVariableWindow(BasePage):
    def __init__(self, page):
        super().__init__(page)
        self.choose_variable_window = self._choose_variable_window()

    def _choose_variable_window(self):
        """选择变量窗口"""
        element = self.page.locator("//div[@role='dialog' and @aria-label='选择变量']")
        return element

    def variable_broadcast_method(self, variable_name, broadcast_method):
        """特定变量的播报方式单选按钮：TTS、录音"""
        xpath = (
            f"//div[@class='el-table__body-wrapper is-scrolling-none']//div[text()='{variable_name}']/parent::td"
            f"/following-sibling::td//span[@class='el-radio__label' and starts-with(text(), '{broadcast_method}')]"
            f"/preceding-sibling::span"
        )
        element = self.choose_variable_window.locator(xpath)
        return element

    def apple_to_speech_button(self, variable_name):
        """【应用到话术】按钮"""
        xpath = (
            f"//div[@class='el-table__fixed-right']//div[text()='{variable_name}']/parent::td"
            f"/following-sibling::td//span[contains(text(), '应用到话术')]/parent::button"
        )
        element = self.choose_variable_window.locator(xpath)
        return element


class StartNode(BasePage):
    """[开始]节点"""

    def __init__(self, page):
        super().__init__(page)

    def tab(self, tab_name="节点模板"):
        """开始节点点击展开后，下面的[节点列表]和[节点模板]tab页"""
        element = self.page.locator(f"//div//nav/div[text()='{tab_name}']")
        return element

    def node_type(self, node_type_name="API节点"):
        """开始节点[节点模板]下的节点类型：澄清节点、API节点、挂机节点"""
        element = self.page.locator(
            f"//div//main/ul/li/span[text()='{node_type_name}']"
        )
        return element

    def button(self, button_name="确定"):
        element = self.page.get_by_role("button", name=button_name)
        return element


class APINode(BasePage):
    """API节点"""

    def __init__(self, page):
        super().__init__(page)
        self.api_node_div = self.page.locator("//div[@class='api-dialog__container']")

    def node_name_input(self):
        """节点名称输入框"""
        element = self.api_node_div.get_by_role("textbox", name="请输入节点名称")
        return element

    def url_input(self):
        """调用url地址输入框"""
        element = self.api_node_div.get_by_role("textbox", name="调用url地址")
        return element

    def param_maneger_dropbox(self):
        """参数管理收展"""
        element = self.api_node_div.locator("//div[text()='参数管理']")
        return element

    def req_param_maneger_textarea(self):
        """输入参数管理输入文本域"""
        element = self.api_node_div.locator(
            "//label[text()='输入参数']/following-sibling::div"
            "//textarea[@class='ace_text-input']"
        )
        return element

    def button(self, button_name="保存"):
        """API节点页面的【新建分支】【取消】【保存】按钮"""
        element = self.api_node_div.get_by_role("button", name=button_name)
        return element

    def custom_button(self):
        """点击【新建分支】后弹出的【自定义】按钮"""
        element = self.page.locator("//li[contains(text(), '自定义')]")
        return element


class ClarifyNode(BasePage):
    """澄清节点"""

    def __init__(self, page):
        super().__init__(page)
        self.clarifu_node_div = self.page.locator(
            "//div[@class='clarify-dialog__container']"
        )

    def node_name_input(self):
        """节点名称输入框"""
        element = self.clarifu_node_div.locator(
            "//input[@placeholder='请输入节点名称']"
        )
        return element

    def button(self, button_name):
        """澄清节点按钮：【新建分支】【取消】【保存】"""
        element = self.clarifu_node_div.get_by_role("button", name=button_name)
        return element

    def custom_button(self):
        """【自定义】按钮"""
        element = self.page.locator("//li[contains(text(), '自定义')]")
        return element


class HangupNode(BasePage):
    """挂机节点"""

    def __init__(self, page):
        super().__init__(page)
        self.node_edit_div = page.locator("//div[@class='hangup-dialog__container']")

    def node_name_input(self):
        """节点名称输入框"""
        element = self.node_edit_div.locator(
            "//label[text()='节点名称']//following-sibling::div//input"
        )
        return element

    def tag(self, tag_name):
        """回复话术标签：禁止打断、用户静默..."""
        element = self.node_edit_div.locator(
            f"//div[normalize-space()='回复话术']//following-sibling::div//"
            "div[@class='base-editor-cotainer__tools-bar__list']//span[text("
            f")='{tag_name}']"
        )
        return element

    def button(self, button_name="确定"):
        element = self.page.get_by_role("button", name=button_name)
        return element


class ConfigManagePage(BasePage):
    """[配置管理]页面元素"""

    def __init__(self, page):
        super().__init__(page)

    def left_tab(self, tab_name):
        """配置管理左侧tab页：变量管理、录音管理、流程话术管理..."""
        element = self.page.locator(
            f"//nav[@class='work-frame__nav']//li[normalize-space()='{tab_name}']"
        )
        return element


class VariableManagePage(BasePage):
    """[变量管理]页面元素"""

    def __init__(self, page):
        super().__init__(page)

    def button(self, button_name="新增字段"):
        """变量管理页面按钮：【刷新字段】【下载模板】【新增字段】"""
        element = self.page.locator(
            f"//h2[text()='变量管理']/following-sibling::div/button"
            f"/span[normalize-space()='{button_name}']"
        )
        return element


class AddVariableWindow(BasePage):
    def __init__(self, page):
        super().__init__(page)
        self.add_variable_window = self.window()

    def window(self):
        """新增变量窗口"""
        element = self.page.locator("//div[@role='dialog' and @aria-label='新增字段']")
        return element

    def field_input(self):
        """字段输入框"""
        element = self.add_variable_window.locator(
            "//label[text()='字段']/following-sibling::div//input"
        )
        return element

    def standard_name_input(self):
        """标准名输入框"""
        element = self.add_variable_window.locator(
            "//label[text()='标准名']/following-sibling::div//input"
        )
        return element

    def default_value(self):
        """默认值输入框"""
        element = self.add_variable_window.locator(
            "//label[text()='默认值']/following-sibling::div//input"
        )
        return element

    def field_type_dropbox(self):
        """字段类型下拉框"""
        element = self.add_variable_window.locator(
            "//label[text()='字段类型']/following-sibling::" "div//input"
        )
        return element

    def field_type_option(self, option_name="其他"):
        """字段类型下拉框可选项"""
        element = self.page.locator(
            f"//div[@class='el-scrollbar']//li/span[text()='{option_name}']"
        )
        return element

    def button(self, button_name="新增"):
        """新增变量窗口按钮：【新增】【取消】"""
        element = self.add_variable_window.locator(
            f"//div/button/span[normalize-space()='{button_name}']"
        )
        return element


class AudioManagePage(BasePage):
    def __init__(self, page):
        super().__init__(page)

    def add_tone_button(self):
        """【添加音色】按钮"""
        element = self.page.locator("//div/button/span[normalize-space()='添加音色']")
        return element

    def tone_pack_name(self, tone_pack_name):
        """音色包列表的音色包名称"""
        element = self.page.locator(
            f"//table//tr[@class='el-table__row']//span[text()='{tone_pack_name}']"
        )
        return element

    def tone_pack_edit_button(self, tone_pack_name):
        """音色包[编辑]按钮"""
        element = self.page.locator(
            f"//span[text()='{tone_pack_name}']/ancestor::tr[@class='el-table__row']"
            "//button[@title='编辑音色']"
        )
        return element

    def tone_pack_del_button(self, tone_pack_name):
        """音色包[删除]按钮"""
        element = self.page.locator(
            f"//span[text()='{tone_pack_name}']/ancestor::tr[@class='el-table__row']"
            "//button[@title='删除事件']"
        )
        return element


class AddtoneWindow(BasePage):
    def __init__(self, page):
        super().__init__(page)
        self._add_tone_window = self._window()

    def _window(self):
        """新建音色包窗口"""
        element = self.page.locator(
            f"//div[@role='dialog' and @aria-label='新建音色包']"
        )
        return element

    def tone_page_name_input(self):
        """音色包名称输入框"""
        element = self._add_tone_window.locator(
            "//input[@placeholder='请输入音色包名称']"
        )
        return element

    def remark_textarea(self):
        """备注文本域"""
        element = self._add_tone_window.locator("//textarea[@placeholder='请输入备注']")
        return element

    def button(self, button_name="确认"):
        """新建音色包窗口上的【确认】【取消】按钮"""
        element = self._add_tone_window.get_by_role("button", name=button_name)
        return element


class TonePackInside(BasePage):
    """音色包内部"""

    def __init__(self, page):
        super().__init__(page)

    def import_record_file_button(self):
        """导入录音文件"""
        element = self.page.get_by_role("button", name="导入录音文件")
        return element

    def normal_recording_upload_area(self):
        """上传录音文件区域"""
        element = self.page.locator("//div[@class='upload-area']//input[@type='file']")
        return element

    def check_all_box(self):
        """全选框"""
        element = self.page.locator(
            "//div[@class='upload-area']//div[@class='el-table__header-wrapper']//label"
            "[@class='el-checkbox']"
        )
        return element

    def normal_recording_import_button(self):
        """[导入]按钮"""
        element = self.page.locator(
            "//div[@aria-label='上传录音']//button/span[contains(text(), '导')]"
        )
        return element

    def tab(self, tab_name="常量录音库"):
        """音色包内部tab页切换:[常量录音库][变量录音库]"""
        element = self.page.locator(
            f"//div[@class='el-tabs__nav-wrap is-top']//div[text()='{tab_name}']"
        )
        return element

    def belong_variable_dropbox(self):
        """所属变量下拉框"""
        element = self.page.locator(
            "//div[@aria-label='导入录音文件']//div[text()='所属变量']/following-sibling::div"
        )
        return element

    def belong_variable_option(self, option):
        """所属变量下拉框可选项"""
        element = self.page.locator(
            f"//div[@class='el-scrollbar']//li/span[text()='{option}']"
        )
        return element

    def variable_recording_upload_area(self):
        """变量录音上传区域"""
        element = self.page.locator("//div[@class='label_item']//input[@type='file']")
        return element

    def variable_recording_import_button(self):
        """变量录音上传窗口[导入]按钮"""
        element = self.page.locator(
            "//div[@aria-label='导入录音文件']//button/span[contains(text(), '导')]"
        )
        return element

    def variable_recording_online_button(self):
        """[变量录音上线]按钮"""
        element = self.page.locator("//button/span[normalize-space()='变量录音上线']")
        return element


class SceneAnalyzeRule(BasePage):
    def __init__(self, page):
        super().__init__(page)
        self._basic_call_analytics_div = self._basic_call_analytics()

    def _basic_call_analytics(self):
        """基础通话分析div"""
        element = self.page.locator(
            "//h2[text()='基础通话分析']/parent::header/following-sibling::section/div"
        )
        return element

    def add_ouput_button(self):
        """[添加输出项]按钮"""
        element = self._basic_call_analytics_div.locator(
            "//button/span[normalize-space()='添加输出项']"
        )
        return element

    def output_item_section(self, analyze_item="客户意向", output_item="输出项1"):
        """分析项下的输出项。举例：客户意向下的输出项1，特殊分析规则下XXX的输出项2"""
        xpath = (
            f"//p[contains(text(), '{analyze_item}')]/../parent::section/parent::header/following-sibling::section"
            f"//p[contains(translate(text(), ' ', ''), '{output_item}')]/../../../parent::section"
        )
        # print(f'分析项下的输出项xpath：{xpath}')
        element = self._basic_call_analytics_div.locator(xpath)
        return element

    @staticmethod
    def fixed_value_input(output_item_element, ouput_item_index=1):
        """固定值输入框"""
        xpath = (
            f"//p[contains(text(), '输出项 {ouput_item_index}')]/"
            f"following-sibling::div[normalize-space()='输出固定值为']//input"
        )
        # print(f'固定值输入框xpath：{xpath}')
        element = output_item_element.locator(xpath)
        return element

    @staticmethod
    def met_condition_dropbox1(output_item_element):
        """输出项下面的第一个：满足全部/任一条件"""
        xpath = "//div[contains(@class, 'trees__header')]//input"
        # print(f'输出项下面的满足(全部|任一)条件xpath：{xpath}')
        element = output_item_element.locator(xpath)
        return element

    def add_condition_button(self):
        """【添加条件】按钮"""
        element = self._basic_call_analytics_div.locator(
            "//button/span[normalize-space()='添加条件']"
        )
        return element

    @staticmethod
    def met_condition_dropbox2(output_item_element, condition_index=1):
        """输出项下面的条件N的满足(全部|任一)条件"""
        xpath = f"//div[contains(translate(text(), ' ', ''), '条件{condition_index}')]/following-sibling::div//input"
        # print(f'输出项下面的条件N的满足(全部|任一)条件xpath：{xpath}')
        element = output_item_element.locator(xpath)
        return element

    @staticmethod
    def condition_dropbox(
        output_item_element,
        condition_name="条件1",
        condition_num="1.1",
        dropbox_index=0,
    ):
        """指定输出项下，指定条件名称，指定条件序号下的，指定序号的条件下拉框"""
        xpath = (
            f"//div[contains(translate(text(), ' ', ''), '{condition_name}')]/../../parent::header"
            f"/following-sibling::div//div[contains(text(), '{condition_num}')]/following-sibling::div//input"
        )
        elements = output_item_element.locator(xpath)
        element = elements.nth(dropbox_index)
        return element

    def condition_option(self, option):
        """定位下拉框可选项，场景分析规则页面的下拉框都适用"""
        xpath = f"//body/div[@class='el-select-dropdown el-popper' and @x-placement]//li/span[text()='{option}']"
        element = self.page.locator(xpath)
        return element

    @staticmethod
    def output_item_button(output_item_element, button_name="确定"):
        """输出项下【确认】【取消】按钮"""
        xpath = f"//button/span[normalize-space()='{button_name}']"
        element = output_item_element.locator(xpath)
        return element

    def buttom_button(self, button_name="保存"):
        """场景分析规则页面底部【保存】【取消】按钮"""
        xpath = f"//main[@class='work-frame__main']//button/span[normalize-space()='{button_name}']"
        element = self.page.locator(xpath)
        return element


class ResourceManagePage(BasePage):
    def __init__(self, page):
        super().__init__(page)
        self._add_num_window = self.add_num_window()

    def add_num_button(self):
        """【添加号码】按钮"""
        xpath = "//button/span[normalize-space()='添加号码']"
        element = self.page.locator(xpath)
        return element

    def add_num_window(self):
        """添加号码窗口"""
        xpath = "//div[@aria-label='添加号码']"
        element = self.page.locator(xpath)
        return element

    def num_dropbox(self):
        """号码下拉框"""
        xpath = "//label[text()='号码']/following-sibling::div//input"
        element = self._add_num_window.locator(xpath)
        return element

    def num_purpose(self, purpose="呼出主叫"):
        """号码用途：呼出主叫、呼入被叫"""
        xpath = f"//span[text()='{purpose}']/preceding-sibling::span"
        element = self._add_num_window.locator(xpath)
        return element

    def add_num_window_button(self, button_name="保存"):
        """添加号码窗口的按钮"""
        xpath = f"//span[normalize-space()='{button_name}']"
        element = self._add_num_window.locator(xpath)
        return element

    def asr_dropbox(self):
        """ASR资源下拉框"""
        element = self.page.get_by_placeholder("请选择ASR资源")
        return element

    def tts_dropbox(self):
        """TTS资源下拉框"""
        xpath = "//p[contains(text(), 'TTS')]/div[@style='width: 280px;']//input[@placeholder='请选择TTS资源']"
        element = self.page.locator(xpath)
        return element

    def play_mode(self, mode="录音+语音合成"):
        """TTS播音模式"""
        xpath = f"//span[text()='{mode}']/preceding-sibling::span"
        element = self.page.locator(xpath)
        return element

    def tone_dropbox(self):
        """音色选择下拉框"""
        element = self.page.get_by_placeholder("音色选择")
        return element

    def speed_input(self):
        """语速输入框"""
        xpath = "//p[contains(text(), '语速选择')]//input"
        element = self.page.locator(xpath)
        return element

    def volume_input(self):
        """音量输入框"""
        xpath = "//p[contains(text(), '音量选择')]//input"
        element = self.page.locator(xpath)
        return element

    def dropbox_option(self, option_name):
        """通用下拉框可选项：号码下拉框、ASR资源下拉框、TTS资源下拉框..."""
        xpath = f"//div[@class='el-scrollbar']//li/span[text()='{option_name}']"
        element = self.page.locator(xpath)
        return element

    def button(self):
        """[资源管理]页面底部的【保存】按钮"""
        xpath = "//button/span[normalize-space()='保存']"
        element = self.page.locator(xpath)
        return element


class IntentManagePage(BasePage):
    """意图管理"""

    def __init__(self, page):
        super().__init__(page)

    def batch_publish(self):
        """【批量发布】按钮"""
        xpath = "//button/span[normalize-space()='批量发布']"
        element = self.page.locator(xpath)
        return element

    def publish_intent(self, publish_way="发布全部意图"):
        """发布意图：发布已选意图、发布全部意图"""
        xpath = f"//ul/li[normalize-space()='{publish_way}']"
        element = self.page.locator(xpath)
        return element

    def toast_message(self, message):
        """toast提示信息"""
        element = self.page.locator(f"text='{message}'")
        return element


class SlotManagePage(BasePage):
    def __init__(self, page):
        super().__init__(page)

    def batch_publish_button(self):
        """【批量发布】按钮"""
        xpath = "//button/span[normalize-space()='批量发布']"
        element = self.page.locator(xpath)
        return element

    def publish_slot(self, publish_way="发布全部词槽"):
        """发布词槽：发布已选词槽、发布全部词槽"""
        xpath = f"//ul/li[normalize-space()='{publish_way}']"
        element = self.page.locator(xpath)
        return element

    def toast_message(self, message):
        """toast提示信息"""
        element = self.page.locator(f"text='{message}'")
        return element


class GlobalEventPage(BasePage):
    def __init__(self, page):
        super().__init__(page)
        self._create_global_event_window = self.create_global_event_window()
        self._global_condition_configure_area = self.global_condition_configure_area()
        self._add_node_window = self.add_node_window()

    def button(self, button_name):
        """【创建全局事件】按钮"""
        xpath = f"//button/span[normalize-space()='{button_name}']"
        element = self.page.locator(xpath)
        return element

    def create_global_event_window(self):
        """创建全局事件窗口"""
        xpath = "//div[@role='dialog' and @aria-label='添加全局事件']"
        element = self.page.locator(xpath)
        return element

    def global_event_name_input(self):
        """创建全局事件窗口上的'全局事件名称'输入框"""
        xpath = "//label[text()='全局事件名称']/following-sibling::div//input"
        element = self._create_global_event_window.locator(xpath)
        return element

    def create_global_event_window_button(self, button_name="确认"):
        """创建全局事件窗口上的【确认】【取消】按钮"""
        xpath = f"//button/span[normalize-space()='{button_name}']"
        element = self._create_global_event_window.locator(xpath)
        return element

    def global_event_card(self, global_event_name):
        """左侧全局事件卡片"""
        xpath = (
            f"//a[@class='globalevent__page__container__main-row-list__item-desc' "
            f"and text()='{global_event_name}']/parent::li"
        )
        element = self.page.locator(xpath)
        return element

    def node(self, node_name):
        """全局事件场景上的节点"""
        xpath = f"//p[text()='{node_name}']/ancestor::*[name()='g']"
        element = self.page.locator(xpath)
        return element

    def node_edit_button(self, node_name):
        """选中节点后，节点上的【编辑】按钮"""
        xpath = (
            f"//p[text()='{node_name}']/ancestor::*[name()='foreignObject']/following-sibling::"
            f"*[name()='use' and @href='#yiflow-bianji1']"
        )
        element = self.page.locator(xpath)
        return element

    def global_condition_configure_area(self):
        """右侧全局条件配置区域"""
        xpath = "//div[@class='event-dialog-container']"
        element = self.page.locator(xpath)
        return element

    def global_condition_configure_area_button(self, button_name):
        """右侧全局条件配置区域所有的按钮"""
        xpath = f"//button/span[normalize-space()='{button_name}']"
        element = self._global_condition_configure_area.locator(xpath)
        return element

    def condition_group(self, group_index=0):
        """条件，包含下拉框和输入框。比如：选择全局参数时，第二个就是输入框；其他大部分都是下拉框"""
        xpath = "//div[@class='condition-editor-container__content-body__item']"
        elements = self._global_condition_configure_area.locator(xpath)
        element = elements.nth(group_index)
        return element

    @staticmethod
    def condition_input(condition_group_element, input_index):
        """定位的某一组条件下的第N个下拉框或者输入框"""
        elements = condition_group_element.locator("//input")
        element = elements.nth(input_index)
        return element

    def condition_met_limit(self):
        """满足条件限制：全部、任一"""
        xpath = "//form[@class='el-form']//div[@class='combine-logic']//input"
        element = self._global_condition_configure_area.locator(xpath)
        return element

    def action_group(self, group_index=0):
        """动作，包含下拉框和输入框。比如：选择'参数'时，第二个就是输入框；其他大部分都是下拉框"""
        xpath = "//div[@class='condition-editor-container__content-operate__container']"
        elements = self._global_condition_configure_area.locator(xpath)
        element = elements.nth(group_index)
        return element

    @staticmethod
    def action_input(action_group_element, input_index):
        """定位的某一组条件下的第N个下拉框或者输入框"""
        elements = action_group_element.locator("//input")
        element = elements.nth(input_index)
        return element

    def add_node_window(self):
        """新增节点窗口"""
        xpath = f"//div[@role='dialog' and @aria-label='新增节点']"
        element = self.page.locator(xpath)
        return element

    def node_name_input(self):
        """节点名称输入框"""
        xpath = f"//label[text()='节点名称']/following-sibling::div//input"
        element = self._add_node_window.locator(xpath)
        return element

    def node_type_dropbox(self):
        """节点类型下拉框"""
        xpath = f"//label[text()='节点类型']/following-sibling::div//input"
        element = self._add_node_window.locator(xpath)
        return element

    def dropbox_option(self, option_name):
        """下拉框可选项"""
        xpath = f"//div[@x-placement]/div[@class='el-scrollbar']//span[text()='{option_name}']"
        element = self.page.locator(xpath)
        return element

    def no_node_options(self):
        """节点下拉框没有可选项"""
        xpath = (
            "//p[@class='el-select-dropdown__empty' and normalize-space()='没有节点']"
        )
        element = self.page.locator(xpath)
        return element

    def add_node_window_button(self, button_name="立即创建"):
        """新增节点窗口上的【立即创建】【取消】按钮"""
        xpath = f"//button/span[normalize-space()='{button_name}']"
        element = self._add_node_window.locator(xpath)
        return element


if __name__ == "__main__":
    ...
